<?php

namespace app\console\controller;

namespace app\console\controller;

use think\Url;
use think\Cache;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\Ad as AdModel;
use app\console\model\NewsType as NewsTypeModel;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Db;

class NewsType extends Controller
{


    /**
     * 前置方法
     */
    protected $beforeActionList = [
        'clearCache' => ['only'],
    ];


    /**
     * 广告位列表
     */
    public function index()
    {
        $param = Request::instance()->param();
        $where = [];
        if(!empty($param)) $where['type_name'] = ['like','%'.$param['title'].'%'];
        $data = NewsTypeModel::order(['sort' => 'asc','id' => 'desc'])->where($where)->where(['auding_status' => 2,'is_delete' => 1])->paginate(10);
        return $this->fetch('', [
            'data'       => $data
        ]);
    }


    /**
     * 广告位创建
     */
    public function create(Request $request, NewsTypeModel $adType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

//            // 验证
//            $validate = Loader::validate('AdType');
//            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $adType->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('index'));
            }

            fail('操作失败');

        }


        return $this->fetch('create', [
            "mate_title" => '广告位添加',
        ]);

    }


    /**
     * 广告位编辑
     */
    public function update($id, Request $request, NewsTypeModel $adType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
           // $validate = Loader::validate('AdType');
           // if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $adType->allowField(true)->save($data, ['id' => $id]);


            if ($dispose !== false) {
                win('操作成功', Url::build('index'));
            }

            fail('操作失败');

        }


        $item = NewsTypeModel::get($id);


        return $this->fetch('create', [
            "mate_title" => '广告位编辑',
            'item'       => $item,


        ]);
    }


    /**
     * 广告位删除
     */
    public function destroy($id, NewsTypeModel $adType)
    {

        if (Request::instance()->isGet()) {
            Db::name('news_type')->where('id',$id)->update(['delete_time' =>date('Y-m-d H:i:s',time()),'is_delete' => 2,'delete_id' => session('manage_id')]);
            win('操作成功', Url::build('index'));
        }
        fail('操作失败');

    }


    /**
     * 栏目全部删除
     */
    public function destroyAll($id, AdTypeModel $adType)
    {

        if ($adType->amputateAll($id)) {

            win('操作成功', Url::build('AdType/index'));
        }

        fail('操作失败');

    }


    /**
     * 栏目状态改变
     */
    public function auditing($id, Request $request, AdTypeModel $adType)
    {
        $id = $request->instance()->param('id');

        $status = $adType->where('id', $id)->value('status') == '1' ? 2 : 1;

        if ($adType->where('id', $id)->update(['status' => $status])) {
            return $status;
        }
        return false;
    }


    /*
    * 排列顺序
    */
    public function sorting()
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {
            (new AdTypeModel)->where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('操作成功', Url::build('AdType/index'));
    }

    /**
     * 清除缓存
     */
    protected function clearCache()
    {
        Cache::rm('AdType');
    }
}